﻿package net.jmp0.toast.levels 
{
	import flash.geom.ColorTransform;
	import net.jmp0.toast.entities.bases.GameButton;
	import net.jmp0.toast.entities.effects.GameText;
	import net.jmp0.toast.entities.effects.LevelCompleteOverlay;
	import net.jmp0.toast.entities.effects.NextLevelButton;
	import net.jmp0.toast.entities.menu.CharSelector;
	import net.jmp0.toast.entities.menu.CreditsButton;
	import net.jmp0.toast.entities.menu.PasswordButton;
	import net.jmp0.toast.entities.menu.SpeechBubbleOverlay;
	import net.jmp0.toast.entities.menu.StartButton;
	import net.jmp0.toast.entities.menu.TitleText;
	import net.jmp0.toast.entities.menu.WhatButton;
	import punk.Actor;
	import punk.Backdrop;
	import punk.core.Spritemap;
	import punk.core.World;

	
	/**
	 * ...
	 * @author Thodd
	 */
	public class PasswordScreen extends World
	{
		//Background
		[Embed(source = '../res/karosheet.png')]private var imgBackgroundKaro:Class;
		private var backgroundKaro:Backdrop = new Backdrop(imgBackgroundKaro, 0, 0, true, true);
		[Embed(source = '../res/coffee_stain_splash.png')]private var imgBackgroundStainSplash:Class;
		private var backgroundStainSplash:Backdrop = new Backdrop(imgBackgroundStainSplash, 0, 0, false, false);
		[Embed(source = '../res/coffee_stain_big.png')]private var imgBackgroundStainBig:Class;
		private var backgroundStainBig:Backdrop = new Backdrop(imgBackgroundStainBig, 0, 0, false, false);
		
		//title text
		private var title:GameText = new GameText("Please enter password:", 150, 30, "kraboudja", 35, -100, true);
		
		//speech bubble overlay
		private var speechBubble:Actor = new SpeechBubbleOverlay(130, 15, 200)
		
		//the character selectors
		private var firstCharSelector:CharSelector;
		private var secondCharSelector:CharSelector;
		private var thirdCharSelector:CharSelector;

		private var nextButton:NextLevelButton;
		
		private var menuButton:NextLevelButton;
		
		public function PasswordScreen() {
		}
		
		
		/**
		 * Initialising the Level
		 */
		override public function init():void {
			
			//Background setup
			setupBackgroundDecals();
			
			//Title text
			this.add(title);

			//Setup the Overlay
			this.add(speechBubble);
			
			//the characterselectors
			firstCharSelector = new CharSelector(190, 130);
			secondCharSelector = new CharSelector(290, 130);
			thirdCharSelector = new CharSelector(390, 130);
			
			//The next level button
			nextButton = new NextLevelButton(300, 300, nextLevelButtonWasPressed);
			nextButton.nextText.text = "check it >";
			
			//The back to main menu button, missusing the nextLevelButton class again :)
			menuButton = new NextLevelButton(430, 430, menuButtonWasPressed);
			menuButton.nextText.text = "forget it";
			
			//cursor
			this.add(new MainMenuCursor());
			
		}
		
		
		private function nextLevelButtonWasPressed(caller:GameButton):void {
			
			var passwordString:String = firstCharSelector.charText.text + secondCharSelector.charText.text + thirdCharSelector.charText.text;
			
			var choosenLevel:XML = LevelTracker.getInstance().findLevelByPassword(passwordString);
			
			if (choosenLevel != null) {
				cleanUp();
				LevelTracker.getInstance().switchToLevelWithMessage(choosenLevel, "That was correct!", "Le shock!");
			}
			
		}
		
		
		private function menuButtonWasPressed(caller:GameButton):void {
			FP.goto = new MainMenu();
		}
		

		/**
		 * Just setting up some coffee stain "decals"
		 */
		private function setupBackgroundDecals():void {
			FP.screen.color = 0xFFFFFF;
			backgroundKaro.depth = 100000;
			backgroundStainSplash.depth = 10000;
			backgroundStainBig.depth = 10000;
			
			backgroundStainSplash.x = -112;
			backgroundStainSplash.y = 160;
			
			backgroundStainBig.x = 464;
			backgroundStainBig.y = -128;
			
			this.add(backgroundKaro);
			this.add(backgroundStainSplash);
			this.add(backgroundStainBig);
		}
		
		
		private function cleanUp():void {
			//title text
			this.remove(title);
		
			//speech bubble overlay
			this.remove(speechBubble);
		
			//the character selectors
			firstCharSelector.destroy();
			secondCharSelector.destroy();
			thirdCharSelector.destroy();

			nextButton.destroy();
		}
		
		
	}

}